package com.lbn.monitor.app.history;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lbn.monitor.R;
import com.lbn.monitor.api.model.Device;
import com.lbn.monitor.app.monitor.adapter.MfListAdapter;
import com.lbn.monitor.base.AppBaseActivity;
import com.lbn.monitor.util.Constant;
import com.lbn.monitor.widget.Appbar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrFrameLayout;
import indi.free.widgets.custom_recycler_view.BasePagerRecyclerAdapter;

public class DeviceListActivity extends AppBaseActivity<DeviceListPresenter> implements IDeviceListView<List<Device>>,
        AppBaseActivity.OnPtrRefresh, BasePagerRecyclerAdapter.OnLoadMoreListener, BasePagerRecyclerAdapter.OnItemClickListener {


    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    private MfListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        ButterKnife.bind(this);
        setAppbar(new Appbar(this)
                .setTitle("设备列表")
                .openBack());

    }


    @Override
    protected DeviceListPresenter createPresenter() {
        return new DeviceListPresenter();
    }

    public static void startActivity(Context context, int req) {
        ((Activity) context).startActivityForResult(new Intent(context, DeviceListActivity.class), req);
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();
        adapter = new MfListAdapter(this, recyclerView, null);
        adapter.setmOnLoadMoreListener(this);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        initPtr(ptrFrame, this);
    }

    int page = 1;

    @Override
    public void onPtrRefresh() {
        page = 1;
        mPresenter.list(page);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.list(page);
    }

    @Override
    public void refreshComplete(List<Device> data) {
        adapter.refreshData(data, true);
        ptrFrame.refreshComplete();
    }

    @Override
    public void addMoreData(List<Device> data, boolean isMore) {
        adapter.addMoreData(data, isMore);
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        Device data = adapter.getItem(pos);
        Intent intent = new Intent();
        intent.putExtra(Constant.DEVICE_DATA, data);
        setResult(RESULT_OK, intent);
        finish();
    }
}
